﻿using Dapper;
using Infrastructure.DataBaseConnect;
using Infrastructure.IRepository.Sys;
using Model.BusinessModel.OutPut.sys;
using Model.DomainModel.Sys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Infrastructure.Repository.Sys
{
    /// <summary>
    /// 用户与角色关系仓储
    /// </summary>
    public class SysUserRoleRelationRepository : Repository<SysUserRoleRelation>, ISysUserRoleRelationRepository
    {
        /// <summary>
        /// 根据用户获取角色关系
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="corporationKey">公司key</param>
        /// <param name="systemKey">系统key</param>
        /// <returns></returns>
        public List<SysUserRoleRelationOutPut> GetSysUserRoleRelationsByUserId(string userId, string corporationKey, string systemKey)
        {
            string sql = @" select ur.*,sr.IsOpen,sr.CorporationKey,sr.SystemKey  from  Sys_UserRoleRelation as ur
                            inner join Sys_Role as sr on ur.RoleId = sr.RoleId
                            where ur.UserId = @UserId and sr.CorporationKey=@CorporationKey and sr.SystemKey=@SystemKey";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            return connection.Query<SysUserRoleRelationOutPut>(sql, new { UserId = userId, CorporationKey = corporationKey, SystemKey = systemKey }).ToList();
        }

        /// <summary>
        /// 根据用户获取角色关系
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="corporationKey">公司key</param>
        /// <returns></returns>
        public List<SysUserRoleRelationOutPut> GetSysUserRoleRelationsByUserId(string userId, string corporationKey)
        {
            string sql = @" select ur.*,sr.IsOpen,sr.CorporationKey,sr.SystemKey  from  Sys_UserRoleRelation as ur
                            inner join Sys_Role as sr on ur.RoleId = sr.RoleId
                            where ur.UserId = @UserId and sr.CorporationKey=@CorporationKey";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            return connection.Query<SysUserRoleRelationOutPut>(sql, new { UserId = userId, CorporationKey = corporationKey }).ToList();
        }

        /// <summary>
        /// 根据用户获取角色关系
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns></returns>
        public List<SysUserRoleRelationOutPut> GetSysUserRoleRelationsByUserId(string userId)
        {
            string sql = @" select ur.*,sr.IsOpen,sr.CorporationKey,sr.SystemKey  from  Sys_UserRoleRelation as ur
                            inner join Sys_Role as sr on ur.RoleId = sr.RoleId
                            where ur.UserId = @UserId ";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            return connection.Query<SysUserRoleRelationOutPut>(sql, new { UserId = userId}).ToList();
        }

        /// <summary>
        /// 根据用户id删除数据
        /// </summary>
        /// <param name="userId">用户id</param>
        public void DeleteByUserId(string userId)
        {
            string sql = " delete from  Sys_UserRoleRelation where UserId=@UserId ";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            connection.Execute(sql, new { UserId = userId });
        }

        /// <summary>
        /// 根据角色获取角色关系
        /// </summary>
        /// <param name="roleId">角色id</param>
        /// <returns></returns>
        public List<SysUserRoleRelationOutPut> GetSysUserRoleRelationsByRoleId(int roleId)
        {
            string sql = @" select * from Sys_UserRoleRelation  where RoleId = @RoleId";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            return connection.Query<SysUserRoleRelationOutPut>(sql, new { RoleId = roleId }).ToList();
        }

        /// <summary>
        /// 根据角色id删除数据
        /// </summary>
        /// <param name="roleId">角色id</param>
        public void DeleteByRoleId(int roleId)
        {
            string sql = " delete from  Sys_UserRoleRelation where RoleId=@RoleId ";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            connection.Execute(sql, new { RoleId = roleId });
        }

        /// <summary>
        /// 新增用户和角色关系
        /// </summary>
        /// <param name="list">新增数据</param>
        public void BatchInsert(List<SysUserRoleRelation> list)
        {
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            connection.Execute(BaseSqlRepository.sysUserRoleRelation_insertSql, list);
        }
    }
}
